/*
 * jlb
 */
package com.jlb.item.service;

import com.jlb.core.entity.Page;
import com.jlb.core.exceptions.BaseException;
import com.jlb.core.exceptions.ItemException;
import com.jlb.item.dao.ItemDAO;
import com.jlb.item.entity.ItemState;
import com.jlb.item.entity.Item;
import com.jlb.core.base.BaseDAO;
import com.jlb.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("itemSV")
@Slf4j
public class ItemSVImpl extends BaseSVImpl<Item, Long> implements ItemSV {

    @Autowired
    private ItemDAO itemDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return itemDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(Item entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        super.save(entity);
    }

    /**
     * 加载一个对象Item
     *
     * @param id * @param code 编码* @param categoryCode 分类编码
     * @return Item
     */
    @Override
    public Item load(Long id, String code, String categoryCode) {
        if (id == null && code == null && categoryCode == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("categoryCode", categoryCode);
        return itemDAO.load(param);
    }

    /**
     * 加载一个对象Item 通过id
     *
     * @param id
     * @return Item
     */
    @Override
    public Item loadById(Long id) {
        if (id == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return itemDAO.loadById(id);
    }

    /**
     * 加载一个对象Item 通过code
     *
     * @param code 编码
     * @return Item
     */
    @Override
    public Item loadByCode(String code) {
        if (code == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return itemDAO.loadByCode(code);
    }

    /**
     * 加载一个对象Item 通过categoryCode
     *
     * @param categoryCode 分类编码
     * @return Item
     */
    @Override
    public Item loadByCategoryCode(String categoryCode) {
        if (categoryCode == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return itemDAO.loadByCategoryCode(categoryCode);
    }


    /**
     * 根据主键id,oldStates 共同更新 Item 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, ItemState newState, ItemState... oldStates) {
        if (id == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        itemDAO.updateStateById(id, new Date(), newState, oldStates);
    }

    /**
     * 根据主键code,oldStates 共同更新 Item 的状态到newState状态
     *
     * @param code      编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByCode(String code, ItemState newState, ItemState... oldStates) {
        if (code == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        itemDAO.updateStateByCode(code, new Date(), newState, oldStates);
    }

    /**
     * 根据主键categoryCode,oldStates 共同更新 Item 的状态到newState状态
     *
     * @param categoryCode 分类编码
     * @param newState     新状态
     * @param oldStates    旧状态集合
     */
    @Override
    public void updateStateByCategoryCode(String categoryCode, ItemState newState, ItemState... oldStates) {
        if (categoryCode == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        itemDAO.updateStateByCategoryCode(categoryCode, new Date(), newState, oldStates);
    }

    /**
     * 根据主键id 更新 Item 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, ItemState state) {
        if (id == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        itemDAO.updateById(id, state, new Date());
    }

    /**
     * 根据主键code 更新 Item 的状态到另一个状态
     *
     * @param code  编码
     * @param state 状态
     */
    @Override
    public void updateByCode(String code, ItemState state) {
        if (code == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        itemDAO.updateByCode(code, state, new Date());
    }

    /**
     * 根据主键categoryCode 更新 Item 的状态到另一个状态
     *
     * @param categoryCode 分类编码
     * @param state        状态
     */
    @Override
    public void updateByCategoryCode(String categoryCode, ItemState state) {
        if (categoryCode == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        itemDAO.updateByCategoryCode(categoryCode, state, new Date());
    }


    /**
     * 删除对象Item
     *
     * @param id * @param code 编码* @param categoryCode 分类编码
     * @return Item
     */
    @Override
    public void delete(Long id, String code, String categoryCode) {
        if (id == null && code == null && categoryCode == null) {
            throw new ItemException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("categoryCode", categoryCode);
        itemDAO.delete(param);
    }


    /**
     * 查询Item分页
     *
     * @param item   对象
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Item>
     */
    @Override
    public List<Item> list(Item item, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (item != null) {
            map = JSON.parseObject(JSON.toJSONString(item, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return itemDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(Item item) {
        Map<String, Object> map = null;
        if (item != null) {
            map = JSON.parseObject(JSON.toJSONString(item, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return itemDAO.count(map);
    }

    /**
     * 查询Item分页
     *
     * @param id     * @param code  编码* @param categoryCode  分类编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Item>
     */
    @Override
    public List<Item> list(Long id, String code, String categoryCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("categoryCode", categoryCode);
        return itemDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String code, String categoryCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("categoryCode", categoryCode);
        return itemDAO.count(param);
    }

}
